package controller;


import dao.impl.PhoneDaoImpl;
import entity.Phone;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.Date;

@WebServlet("/addPhone.do")
public class AddPhoneServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String inventory = req.getParameter("inventory");
        String name = req.getParameter("name");
        String model = req.getParameter("model");
        BigDecimal price = BigDecimal.valueOf(Double.valueOf(req.getParameter("price")));
        Date time = new Date();
        PhoneDaoImpl phoneDaoImpl = new PhoneDaoImpl();
        try {
            phoneDaoImpl.insertPhone(new Phone(Integer.parseInt(inventory),name,model,price,time));
            System.out.println("添加成功！");
            resp.sendRedirect("/findAllPhone.do");
        }catch (SQLException e){
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
